//
//  SSUserAccountModel.swift
//  SSSina
//
//  Created by ss on 2017/7/1.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit

//记得要遵守nscoder协议
class SSUserAccountModel: NSObject ,NSCoding{
    ///用户唯一授权票据
    var access_token:String?
    ///access_token的声明周期,能使用多少秒
    var expires_in:Double = 0{
        //监听外界赋值
        didSet{
            //过期的日期 = 当前的日期 + 服务器提供的秒数
            expires_Data = Date(timeIntervalSinceNow: expires_in)
        }
    }
    ///授权用户的UID
    var uid:String?
    ///用户昵称
    var screen_name:String?
    ///用户头像
    var avatar_large:String?
    ///用来判断用户token过期时间(普通用户三天,会员五年)
    var expires_Data:Date?
    
    //KVC 构造函数
    init(dic:[String:Any]) {
        super.init()
        setValuesForKeys(dic)
    }
    //避免属性不对应报错
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    override var description: String{
        //keys
        let keys = ["access_token","expires_in","uid","screen_name","avatar_large"]
        return dictionaryWithValues(forKeys: keys).description
    }
    
    //归档方法 ->属性存二进制保存到磁盘
    func encode(with aCoder:NSCoder){
        aCoder.encode(access_token,forKey:"access_token")
        aCoder.encode(expires_Data,forKey:"expires_Data")
        aCoder.encode(uid,forKey:"uid")
        aCoder.encode(screen_name,forKey:"screen_name")
        aCoder.encode(avatar_large,forKey:"avatar_large")
    }
    
    //解档 -> 从磁盘中获取二进制变成属性
    required  init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_Data = aDecoder.decodeObject(forKey: "expires_Data") as? Date
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String

    }
}
